/**
 * Created with JetBrains PhpStorm.
 * User: T420
 * Date: 12/12/12
 * Time: 11:28 PM
 * To change this template use File | Settings | File Templates.
 */
function gmobj(o){
    if(document.getElementById){ m=document.getElementById(o); }
    else if(document.all){ m=document.all[o]; }
    else if(document.layers){ m=document[o]; }
    return m;
}

function getNodeValue(o){
    try	{
        return o.item(0).firstChild.nodeValue;
    }
    catch(err) {
        return '';
    }
}

function TrimAndRDS(iStr)
{
    function IsHyperLink(iStr)
    {
        var i = 0, c = ' ';

        if (iStr.charAt(0) == '.')
            return false;

        for (i=0; i < iStr.length; i++)
        {
            c = iStr.charAt(i).toLowerCase();
            if (c >= '0' && c <= '9')

                continue;

            if (c >= 'a' && c <= 'z')
                continue;

            if ('@_-&.?#+-/:'.indexOf(c) != -1)
                continue;

            return false;
        }

        return true;
    }

    function GetLastBreak(iStr, s)
    {
        var f = new Array('(', ')', '<', '>', ' ', '\r', '\n', '\t', ',', ';', '!'), p = 0, i = 0, r = -1;

        for (i = 0; i < f.length; i++)
            if ((p = iStr.lastIndexOf(f[i], s)) != -1)
                if (r == -1 || p > r)
                    r = p;
        return r;
    }

    function GetNextBreak(iStr, s)
    {
        var f = new Array('(', ')', '<', '>', ' ', '\r', '\n', '\t', ',', ';', '!'), p = 0, i = 0, r = -1;

        for (i = 0; i < f.length; i++)
            if ((p = iStr.indexOf(f[i], s)) != -1)
                if (r == -1 || p < r)
                    r = p;
        return r;
    }

    function CheckDotAfter(iStr)
    {
        var p0 = 0, p1 = 0, p2 = 0, p3 = 0;

        while ((p1 = iStr.indexOf('.', p0)) != -1)
        {
            if (iStr.charAt(p1 - 1) == ' ')
            {
                iStr = iStr.substr(0, p1 - 1).concat(iStr.substr(p1));
                p0 = p1;
            }
            else
            {
                p0 = p1 + 1;
            }

            if (iStr.charAt(p0) != ' ')
            {
                if ((p3 = GetLastBreak(iStr, p0)) == -1)
                {
                    p3 = p0;
                }
                else
                {
                    p3 = p3 + 1;
                }

                if ((p2 = GetNextBreak(iStr, p3)) == -1)
                {
                    if (IsHyperLink(iStr.substr(p3)))
                    {
                        iStr = iStr.substr(0, p3).concat(iStr.substr(p3).toLowerCase())
                        break;
                    }
                    else
                    {
                        if (iStr.charAt(p0) < '0' || iStr.charAt(p0) > '9')
                        {
                            iStr = iStr.substr(0, p0).concat(' ').concat(iStr.substr(p0, 1).toUpperCase()).concat(iStr.substr(p0 + 1));
                            p0++;
                        }
                    }
                }
                else
                {
                    if (IsHyperLink(iStr.substring(p3, p2)))
                    {
                        iStr = iStr.substr(0, p3).concat(iStr.substring(p3, p2).toLowerCase()).concat(iStr.substr(p2));
                        p0 = p2 + 1;
                    }
                    else
                    {
                        if (iStr.charAt(p0) < '0' || iStr.charAt(p0) > '9')
                        {
                            iStr = iStr.substr(0, p0).concat(' ').concat(iStr.substr(p0, 1).toUpperCase()).concat(iStr.substr(p0 + 1));
                            p0++;
                        }
                    }
                }
            }
            else
            {
                iStr = iStr.substr(0, p0 + 1).concat(iStr.substr(p0 + 1, 1).toUpperCase()).concat(iStr.substr(p0 + 2));
            }
        }

        return iStr;
    }

    function CheckCharAfter(iStr, iChar, iUp)
    {
        var p0 = 0, p1 = 0;

        while ((p1 = iStr.indexOf(iChar, p0)) != -1)
        {
            if (iStr.charAt(p1 - 1) == ' ')
            {
                iStr = iStr.substr(0, p1 - 1).concat(iStr.substr(p1));
                p0 = p1;
            }
            else
            {
                p0 = p1 + 1;
            }

            if (iStr.charAt(p0) != ' ')
            {
                if (iStr.charAt(p0) < '0' || iStr.charAt(p0) > '9')
                {
                    if (iUp)
                    {
                        iStr = iStr.substr(0, p0).concat(' ').concat(iStr.substr(p0, 1).toUpperCase()).concat(iStr.substr(p0 + 1));
                    }
                    else
                    {
                        iStr = iStr.substr(0, p0).concat(' ').concat(iStr.substr(p0));
                    }
                    p0++;
                }
            }
            else
            {
                if (iUp)
                {
                    iStr = iStr.substr(0, p0 + 1).concat(iStr.substr(p0 + 1, 1).toUpperCase()).concat(iStr.substr(p0 + 2));
                }
            }
        }

        return iStr;
    }

    function CheckScope(iStr, s1, s2)
    {
        var p0 = 0, p1 = 0;

        for (p0 = 0; (p1 = iStr.indexOf(s1, p0)) != -1; )
        {
            if (iStr.charAt(p1 + 1) == ' ')
                iStr = iStr.substr(0, p1 + 1).concat(iStr.substr(p1 + 2));

            if (p1 > 0)
                if (iStr.charAt(p1 - 1) != ' ')
                {
                    iStr = iStr.substr(0, p1).concat(' ').concat(iStr.substr(p1));
                    p1++;
                }

            p0 = p1 + 1;
        }

        for (p0 = 0; (p1 = iStr.indexOf(s2, p0)) != -1; )
        {
            var SkipChar = ':,.;!?'.concat(s2);

            if (p1 > 0)
                if (iStr.charAt(p1 - 1) == ' ')
                {
                    iStr = iStr.substr(0, p1 - 1).concat(iStr.substr(p1));
                    p1--;
                }

            if (iStr.charAt(p1 + 1) != ' ' && SkipChar.indexOf(iStr.charAt(p1 + 1)) == -1)
                iStr = iStr.substr(0, p1 + 1).concat(' ').concat(iStr.substr(p1 + 1));

            p0 = p1 + 1;
        }

        return iStr;
    }

    iStr = ReplaceAll(iStr, '  ', ' ');
    iStr = ReplaceAll(iStr, ' \r\n', '\r\n');
    iStr = ReplaceAll(iStr, '\r\n ', '\r\n');

    iStr = CheckCharAfter(iStr, ',', false);
    iStr = CheckCharAfter(iStr, ':', false);
    iStr = CheckCharAfter(iStr, ';', false);
    iStr = CheckCharAfter(iStr, '?', true);
    iStr = CheckCharAfter(iStr, '!', true);

    iStr = CheckScope(iStr, '(', ')');
    iStr = CheckScope(iStr, '[', ']');

    iStr = ReplaceAll(iStr, 'http: //', 'http://');
    iStr = CheckDotAfter(iStr);

    iStr = ReplaceAll(iStr, ', \r\n', ',\r\n');
    iStr = ReplaceAll(iStr, ': \r\n', ':\r\n');
    iStr = ReplaceAll(iStr, '; \r\n', ';\r\n');
    iStr = ReplaceAll(iStr, '? \r\n', '!\r\n');
    iStr = ReplaceAll(iStr, '! \r\n', '!\r\n');
    iStr = ReplaceAll(iStr, '. \r\n', '.\r\n');


    if (iStr.charAt(0) == ' ')
        iStr = iStr.substr(1);

    if (iStr.charAt(iStr.length - 1) == ' ')
        iStr = iStr.substr(0, iStr.length - 1);

    return iStr.substr(0, 1).toUpperCase().concat(iStr.substr(1));
}

function ReplaceAll(iStr, v1, v2)
{
    var i = 0, oStr = '', j = v1.length;

    while (i < iStr.length)
    {
        if (iStr.substr(i, j) == v1)
        {
            oStr+=v2;
            i+=j
        }
        else
        {
            oStr+=iStr.charAt(i);
            i++;
        }
    }

    return oStr;
}

function CheckEmailAddress(Email)
{
    Email = Trim(Email);

    while (Email != '')
    {
        c = Email.charAt(0);
        if (c==' ' || c=='<' || c==39 || c==':' || c=='.')
        {
            Email = Email.substr(1);
        }
        else
        {
            break;
        }
    }

    i = Email.indexOf('>');
    if (i==-1)
    {
        while (Email != '')
        {
            c = Email.charAt(Email.length - 1);
            if (c==' ' || c==39 || c=='.')
            {
                Email = Email.substr(0, Email.length - 1);
            }
            else
            {
                break;
            }
        }
    }
    else
    {
        Email = Email.substr(0, i);
    }

    if (Email.length > 96)
        return '';

    i = Email.lastIndexOf('@');
    j = Email.lastIndexOf('.');
    if (i < j)
        i = j;

    switch (Email.length - i - 1)
    {
        case 2:
            break;
        case 3:
            switch (Email.substr(i))
            {
                case '.com':
                case '.net':
                case '.org':
                case '.edu':
                case '.mil':
                case '.gov':
                case '.biz':
                case '.pro':
                case '.int':
                    break;
                default:
                    return '';
            }
            break;
        default:
            switch (Email.substr(i))
            {
                case '.name':
                case '.info':
                    break;
                default:
                    return '';
            }
            break;
    }

    Email = Email.toLowerCase();

    if (Email == '')
        return '';

    if (Email.indexOf(' ') != -1)
        return '';

    if (Email.indexOf('..') != -1)
        return '';

    if (Email.indexOf('.@') != -1)
        return '';

    if (Email.indexOf('@.') != -1)
        return '';

    if (Email.indexOf(':') != -1)
        return '';

    for (i=0; i < Email.length; i++)
    {
        c = Email.charAt(i);

        if (c >= '0' && c <= '9')
            continue;

        if (c >= 'a' && c <= 'z')
            continue;

        if ('`~!#$%^&*-_+=?/\\|@.'.indexOf(c) != -1)
            continue;

        return '';
    }

    if ((i=Email.indexOf('@'))==-1)
        return '';

    if (Email.substr(i + 1).indexOf('@')!=-1)
        return '';

    if (Email.charAt(0)=='.' || Email.charAt(Email.length - 1)=='.')
        return '';

    return Email;
}

function Trim(iStr)
{
    while (iStr.charCodeAt(0) <= 32)
    {
        iStr=iStr.substr(1);
    }

    while (iStr.charCodeAt(iStr.length - 1) <= 32)
    {
        iStr=iStr.substr(0, iStr.length - 1);
    }

    return iStr;
}

function showVideoObject(title, path, imagepath) {
    gmobj('video-title').innerHTML 	= title;
    var so = new SWFObject('/Themes/Default/Client/flash/mediaplayer.swf','MediaPlayer','278','231','8');
    so.addParam('allowscriptaccess','always');
    so.addParam('allowfullscreen','true');
    so.addParam('wmode','transparent');
    so.addVariable('width','278');
    so.addVariable('height','231');
    so.addVariable('file', path);
    if(imagepath==''){
        //so.addVariable('image','/Themes/Default/Client/images/video.jpg');
    }
    else{
        so.addVariable('image', imagepath);
    }
    so.write('mediaspace');
}

function openMeExt(vLink, vStatus, vResizeable, vScrollbars, vToolbar, vLocation, vFullscreen, vTitlebar, vCentered, vHeight, vWidth, vTop, vLeft, vID, vCounter)
{
    var sLink = (typeof(vLink.href) == 'undefined') ? vLink : vLink.href;

    winDef = '';
    winDef = winDef.concat('status=').concat((vStatus) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('resizable=').concat((vResizeable) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('scrollbars=').concat((vScrollbars) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('toolbar=').concat((vToolbar) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('location=').concat((vLocation) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('fullscreen=').concat((vFullscreen) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('titlebar=').concat((vTitlebar) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('height=').concat(vHeight-140).concat(',');
    winDef = winDef.concat('width=').concat(vWidth).concat(',');

    if (vCentered){
        winDef = winDef.concat('top=').concat((screen.height - vHeight)/2).concat(',');
        winDef = winDef.concat('left=').concat((screen.width - vWidth)/2);
    }
    else{
        winDef = winDef.concat('top=').concat(vTop).concat(',');
        winDef = winDef.concat('left=').concat(vLeft);
    }

    if (typeof(vCounter) == 'undefined'){
        vCounter = 0;
    }

    if (typeof(vID) == 'undefined')	{
        vID = 0;
    }

    if (vCounter){
        sLink = buildLink(vID,sLink);
    }

    open(sLink, '_blank', winDef);

    if (typeof(vLink.href) != 'undefined')	{
        return false;
    }
}

function EmailSubject(sURL)
{
    openMeExt(sURL + '?u='.concat(escape(location.href)), 0, 0, 0, 0, 0, 0, 1, 1, 415, 480, 0, 0, '', 0);
}

function Update_Cart(pid, count)
{
    var ranNum = Math.floor(Math.random() * 999999);

    sLink = '/Tools/Cart.aspx?cmd=update_cart&pid=' + pid + '&c=' + count + '&rnd=' + ranNum;
    AjaxRequest.get(
        {
            'url':sLink
            ,'onSuccess':function(req)
        {
            alert(CART_TITLE);
            ViewCart();
        }
            ,'onError':function(req){}
        }
    )
}

function ShowWeatherBox(vId){
    var sLink = '';
    sLink = '/COMA/Weather_JS/GetData.asp?URL=http://vnexpress.net/ListFile/Weather/';
    switch (parseInt(vId)){
        case 1: sLink = sLink.concat('Sonla.xml');break;
        case 2: sLink = sLink.concat('Viettri.xml');break;
        case 3: sLink = sLink.concat('Haiphong.xml');break;
        case 4: sLink = sLink.concat('Hanoi.xml');break;
        case 5: sLink = sLink.concat('Vinh.xml');break;
        case 6: sLink = sLink.concat('Danang.xml');break;
        case 7: sLink = sLink.concat('Nhatrang.xml');break;
        case 8: sLink = sLink.concat('Pleicu.xml');break;
        case 9: sLink = sLink.concat('HCM.xml');break;
        default: sLink = sLink.concat('Hanoi.xml');break;
    }
    //sLink = '/COMA/Weather_JS/sonla.xml'
    AjaxRequest.get(
        {
            'url':sLink
            ,'onSuccess':function(req){
            var vAdImg, vAdImg1, vAdImg2, vAdImg3, vAdImg4, vAdImg5, vWeather;
            vAdImg = req.responseXML.getElementsByTagName('AdImg').item(0).firstChild.nodeValue;
            vAdImg1 = req.responseXML.getElementsByTagName('AdImg1').item(0).firstChild.nodeValue;
            if(req.responseXML.getElementsByTagName('AdImg2').item(0).firstChild != null)
                vAdImg2 = req.responseXML.getElementsByTagName('AdImg2').item(0).firstChild.nodeValue;
            if(req.responseXML.getElementsByTagName('AdImg3').item(0).firstChild != null)
                vAdImg3 = req.responseXML.getElementsByTagName('AdImg3').item(0).firstChild.nodeValue;
            if(req.responseXML.getElementsByTagName('AdImg4').item(0).firstChild != null)
                vAdImg4 = req.responseXML.getElementsByTagName('AdImg4').item(0).firstChild.nodeValue;
            if(req.responseXML.getElementsByTagName('AdImg5').item(0).firstChild != null)
                vAdImg5 = req.responseXML.getElementsByTagName('AdImg5').item(0).firstChild.nodeValue;
            vWeather = req.responseXML.getElementsByTagName('Weather').item(0).firstChild.nodeValue;
            GetWeatherBox(vAdImg, vAdImg1, vAdImg2, vAdImg3, vAdImg4, vAdImg5, vWeather);
        }
            ,'onError':function(req){}
        }
    )
}

function GetWeatherBox(vImg, vImg1, vImg2, vImg3, vImg4, vImg5, vWeather){
    var sHTML = '';
    sHTML = sHTML.concat('<img src="http://vnexpress.net/Images/Weather/').concat(vImg).concat('" class="img-weather" style="height:30px;" alt="" />&nbsp;');
    sHTML = sHTML.concat('<img src="http://vnexpress.net/Images/Weather/').concat(vImg1).concat('" class="img-weather" alt="" />');
    if(vImg2!=null) sHTML = sHTML.concat('<img src="http://vnexpress.net/Images/Weather/').concat(vImg2).concat('" class="img-weather" alt="" />');
    if(vImg3!=null) sHTML = sHTML.concat('<img src="http://vnexpress.net/Images/Weather/').concat(vImg3).concat('" class="img-weather" alt="" />');
    if(vImg4!=null) sHTML = sHTML.concat('<img src="http://vnexpress.net/Images/Weather/').concat(vImg4).concat('" class="img-weather" alt="" />');
    if(vImg5!=null) sHTML = sHTML.concat('<img src="http://vnexpress.net/Images/Weather/').concat(vImg5).concat('" class="img-weather" alt="" />');
    sHTML = sHTML.concat('<img src="http://vnexpress.net/Images/Weather/c.gif" class="img-weather" style="height:20px;" alt="" />');

    if(Weather_Replace != '')
    {
        var s1 = Weather_Replace.split(",");
        for(var i=0;i<s1.length;i++)
        {
            var s2 = s1[i].split("|");
            vWeather = vWeather.replace(s2[0], s2[1]);
        }
    }

    gmobj('img-Do').innerHTML = sHTML;
    gmobj('txt-Weather').innerHTML = vWeather;
}

function ShowGoldPrice(){
    var sHTML = '';
    sHTML = sHTML.concat('<span style="color:#8A0000;font:bold 10px arial;">'+Weather_Unit+'</span>');
    sHTML = sHTML.concat('<table border="0px" cellpadding="2px" cellspacing="1px" class="tbl-goldprice">');
    sHTML = sHTML.concat('	<tr>');
    sHTML = sHTML.concat('		<td class="td-weather-title" style="font-size:10px;width:46%;">'+Weather_GoldType+'</td>');
    sHTML = sHTML.concat('		<td class="td-weather-title" style="text-align:center;font-size:10px;width:27%;">'+Weather_Buy+'</td>');
    sHTML = sHTML.concat('		<td class="td-weather-title" style="text-align:center;font-size:10px;width:27%;">'+Weather_Sell+'</td>');
    sHTML = sHTML.concat('	</tr>');
    sHTML = sHTML.concat('	<tr>');
    sHTML = sHTML.concat('		<td class="td-weather-title">SBJ</td>');
    sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vGoldSbjBuy).concat('</td>');
    sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vGoldSbjSell).concat('</td>');
    sHTML = sHTML.concat('	</tr>');
    sHTML = sHTML.concat('	<tr>');
    sHTML = sHTML.concat('		<td class="td-weather-title">SJC</td>');
    sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vGoldSjcBuy).concat('</td>');
    sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vGoldSjcSell).concat('</td>');
    sHTML = sHTML.concat('	</tr>');
    sHTML = sHTML.concat('</table>');
    gmobj('eGold').innerHTML = sHTML;
}

function ShowForexRate(){
    var sHTML = '';
    sHTML = sHTML.concat('<table border="0px" cellpadding="2px" cellspacing="1px" class="tbl-weather">');
    for(var i=0;i<vForexs.length;i++){
        sHTML = sHTML.concat('	<tr>');
        sHTML = sHTML.concat('		<td class="td-weather-title">').concat(vForexs[i]).concat('</td>');
        sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vCosts[i]).concat('</td>');
        sHTML = sHTML.concat('	</tr>');
    }
    sHTML = sHTML.concat('</table>');
    gmobj('eForex').innerHTML = sHTML;
}